package com.rjwh.dingdong.client.mvp.presenter.impl;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;

import com.aishua.lib.task.AsResult;
import com.rjwh.dingdong.client.mvp.BasePresenter;
import com.rjwh.dingdong.client.mvp.presenter.IPlayerToolbarPresenter;
import com.rjwh.dingdong.client.mvp.view.PlayerToolbarView;
import com.rjwh.dingdong.client.player.PlayConstants;
import com.rjwh.dingdong.client.player.PlayMode;
import com.rjwh.dingdong.client.service.PlaybackService;
import com.rjwh.dingdong.client.util.PreferenceManager;

/**
 * 项目名称：whrjwh-parent
 * 类描述：
 * 创建人：wby
 * 创建时间：2016/10/26 9:55
 * 修改人：wby
 * 修改时间：2016/10/26 9:55
 * 修改备注：
 *
 * @version V1.0
 */
public class PlayerToolbarPresenter extends BasePresenter<PlayerToolbarView> implements IPlayerToolbarPresenter {

    private PlaybackService mPlaybackService;
    private boolean mIsServiceBound;

    public PlayerToolbarPresenter(Context context, PlayerToolbarView mMvpView) {
        super(context, mMvpView);
    }

    @Override
    public void onServerDataAcquired(int businessType, AsResult mAsResult, boolean isRefresh) {

    }

    @Override
    public void detachView() {
        unbindPlaybackService();
        detachViews();
    }

    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            // This is called when the connection with the service has been
            // established, giving us the service object we can use to
            // interact with the service.  Because we have bound to a explicit
            // service that we know is running in our own process, we can
            // cast its IBinder to a concrete class and directly access it.
            mPlaybackService = ((PlaybackService.LocalBinder) service).getService();
            if(mPlaybackService!=null) {
                getMvpView().onPlaybackServiceBound(mPlaybackService);
                getMvpView().onSongUpdated(mPlaybackService.getPlayingSong());
            }
            }

        public void onServiceDisconnected(ComponentName className) {
            mPlaybackService = null;
            getMvpView().onPlaybackServiceUnbound();
        }
    };

    public void subscribe() {
        bindPlaybackService();

        retrieveLastPlayMode();
//
//        // TODO
//        if (mPlaybackService != null && mPlaybackService.isPlaying()) {
//            getMvpView().onSongUpdated(mPlaybackService.getPlayingSong());
//        } else {
//            // - load last play list/folder/song
//        }
    }


    public void retrieveLastPlayMode() {
        PlayMode lastPlayMode = PreferenceManager.lastPlayMode(getContext());
        getMvpView().updatePlayMode(lastPlayMode);
    }


    public void bindPlaybackService() {
        getContext().bindService(new Intent(getContext(), PlaybackService.class), mConnection, Context.BIND_AUTO_CREATE);
        mIsServiceBound = true;
    }

    public void unbindPlaybackService() {
        if (mIsServiceBound) {
            // Detach our existing connection.
            getContext().unbindService(mConnection);
            mIsServiceBound = false;
        }
    }
    // 播放
    public void startPlayMusic() {
        Intent intent = new Intent(getContext(), PlaybackService.class);
        intent.setAction(PlayConstants.ACTION_PLAY_TOGGLE);
        getContext().startService(intent);
    }

    // 暂停
    public void pausePlayMusic() {
        Intent intent = new Intent(getContext(), PlaybackService.class);
        intent.setAction(PlayConstants.ACTION_STOP_SERVICE);
        getContext().startService(intent);
    }

    // 播放下一首
    public void playNextMusic() {
        Intent intent = new Intent(getContext(), PlaybackService.class);
        intent.setAction(PlayConstants.ACTION_PLAY_NEXT);
        getContext().startService(intent);
    }

    // 播放上一首
    public void playPrevMusic() {
        Intent intent = new Intent(getContext(), PlaybackService.class);
        intent.setAction(PlayConstants.ACTION_PLAY_LAST);
        getContext().startService(intent);
    }

    public void seek(int prog){
        Intent intent = new Intent(getContext(), PlaybackService.class);
        intent.setAction(PlayConstants.ACTION_SEEK_TO);
        intent.putExtra("prog",prog);
        getContext().startService(intent);
    }
}
